package com.andy.blog.admin.service.impl;

import com.andy.blog.admin.dto.AlbumPhotoDTO;
import com.andy.blog.admin.service.AlbumPhotoBusService;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.AlbumPhotoDO;
import com.andy.blog.dal.service.AlbumPhotoDalService;
import com.andy.blog.dal.vo.AlbumPhotoVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by andy on 2024/4/2 12:26
 **/
@Service
public class AlbumPhotoBusServiceImpl implements AlbumPhotoBusService {

    @Resource
    private AlbumPhotoDalService albumPhotoDalService;

    @Override
    public List<AlbumPhotoDTO> selectList() {

        List<AlbumPhotoDO> albumPhotoDOS = albumPhotoDalService.selectList(new AlbumPhotoVO());

        List<AlbumPhotoDTO> dataList = new ArrayList<>();
        List<AlbumPhotoDO> partentList = albumPhotoDOS.stream().filter(s -> Objects.equals(0L, s.getParentId())).collect(Collectors.toList());
        for (AlbumPhotoDO albumPhotoDO : partentList) {
            AlbumPhotoDTO albumPhotoDTO = TransformUtils.transform(albumPhotoDO, AlbumPhotoDTO.class);
            albumPhotoDTO.setChildren(buildChildren(albumPhotoDO.getId(), albumPhotoDOS));
            dataList.add(albumPhotoDTO);
        }

        return dataList;
    }

    @Override
    public List<AlbumPhotoDTO> listByParent() {
        List<AlbumPhotoDO> albumPhotoDOS = albumPhotoDalService.selectList(AlbumPhotoVO.builder().parentId(0L).build());
        return TransformUtils.transformList(albumPhotoDOS, AlbumPhotoDTO.class);
    }

    @Override
    public void add(AlbumPhotoVO albumPhotoVO) {
        // 如果是相册，需要校验相册是否已存在
        if (Objects.equals(0L, albumPhotoVO.getParentId()) && albumPhotoDalService.checkExists(albumPhotoVO.getPhotoType())) {
            throw new BlogServiceException(BlogResultCode.BLOG_000009);
        } else {
            Long parentId = albumPhotoVO.getParentId();
            AlbumPhotoDO albumPhotoDO = albumPhotoDalService.selectById(parentId);
            Optional.ofNullable(albumPhotoDO).map(AlbumPhotoDO::getPhotoType).ifPresent(albumPhotoVO::setPhotoType);
        }

        albumPhotoDalService.add(albumPhotoVO);
    }

    @Override
    public void deleteById(Long id) {
        AlbumPhotoDO albumPhotoDO = albumPhotoDalService.selectById(id);

        if (Objects.equals(0L, albumPhotoDO.getParentId())) {
            List<AlbumPhotoDO> albumPhotoDOS = albumPhotoDalService.selectList(AlbumPhotoVO.builder().parentId(albumPhotoDO.getId()).build());
            if (CollectionUtils.isNotEmpty(albumPhotoDOS)) {
                throw new BlogServiceException(BlogResultCode.BLOG_000008);
            }
        }

        albumPhotoDalService.deleteById(id);
    }

    @Override
    public void updateById(AlbumPhotoVO albumPhotoVO) {
        if (Objects.isNull(albumPhotoVO.getParentId())) {
            albumPhotoVO.setParentId(0L);
        }

        albumPhotoDalService.updateById(albumPhotoVO);
    }

    @Override
    public AlbumPhotoDTO selectById(Long id) {
        AlbumPhotoDO albumPhotoDO = albumPhotoDalService.selectById(id);

        return TransformUtils.transform(albumPhotoDO, AlbumPhotoDTO.class);
    }

    private List<AlbumPhotoDTO> buildChildren(Long id, List<AlbumPhotoDO> albumPhotoDOS) {

        List<AlbumPhotoDO> collect = albumPhotoDOS.stream().filter(s -> Objects.equals(id, s.getParentId())).collect(Collectors.toList());

        return TransformUtils.transformList(collect, AlbumPhotoDTO.class);
    }
}
